<?php

namespace App\Http\Services;

use App\Models\Paper;
use App\Models\PaperClass;
use Auth;
use Illuminate\Database\Eloquent\Model;

class PaperService
{
    public function edit($item)
    {
        $data = new Paper();
        if(isset($item['id']) && $item['id'] > 0){
            $data = Paper::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }
        $problem_list = json_decode($item['problem_list']);
        $score = 0;
        foreach ($problem_list as $v) {
            $score += $v->mark;
        }
        $data->title = $item['title'];
        $data->problem_list = $item['problem_list'];
        $data->score = $score;
        $data->num = count($problem_list);
        $data->cid = $item['cid'];;
        $data->eid = 0;
        $data->is_xz = 1;
        $data->source_id = 0;
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function typeStore($item)
    {
        $data = new PaperClass();
        $data->create_time = time();
        $data->title = $item['title'];
        $data->eid = 0;
        $data->sid = 1;
        $data->pid = isset($item['pid']) ? $item['pid'] : 0;
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function typeUpdate($item)
    {
        $data = new PaperClass();
        if(isset($item['id']) && $item['id'] > 0){
            $data = PaperClass::find($item['id']);
            if(!$data){
                return false;
            }
        }else{
            $data->create_time = time();
        }
        $data->title = $item['title'];
        $data->eid = 0;
        $data->sid = 1;
        $data->update_time = time();
        $data->save();
        if($data->id){
            return true;
        }
        return  false;
    }

    public function typeTo($item)
    {
        $paper_id = explode(',',$item['paper_id']);
        Paper::whereIn('id',$paper_id)->update(['cid'=>$item['transfer_to_id']]);
        return  true;
    }
    
    public function typeDelete($id)
    {
        $data = PaperClass::find($id);
        $data->delete();
        if($data->id){
            return true;
        }
        return  false;
    }
    
    
}
